package com.example.camera_study;

import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.Manifest.permission_group.CAMERA;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    String TAG ="MainActivity";
CameraManager cameraManager;//相机管理者
    CameraCharacteristics characteristics;
    ToggleButton open1;
    ToggleButton open2;
    ToggleButton take1;
    ToggleButton take2;
    ToggleButton recoder1;
    ToggleButton recoder2;
    ToggleButton test_record;

    CameraUtils cameraUtils1;
    CameraUtils cameraUtils2;
    private HandlerThread mBackgroundforcamera0Thread=null;
    private Handler mBackgroundforcamera0Handler;
    private HandlerThread mBackgroundforcamera1Thread=null;
    private Handler mBackgroundforcamera1Handler;
    EditText test_times;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch(msg.what){
                case 1:
                    mBackgroundforcamera0Thread=new HandlerThread("0");
                    mBackgroundforcamera0Thread.start();
                    mBackgroundforcamera0Handler = new Handler(mBackgroundforcamera0Thread.getLooper());

                    mBackgroundforcamera1Thread=new HandlerThread("1");
                    mBackgroundforcamera1Thread.start();
                    mBackgroundforcamera1Handler = new Handler(mBackgroundforcamera1Thread.getLooper());

                    cameraUtils1=new CameraUtils(cameraManager,"0",MainActivity.this,mBackgroundforcamera0Thread,mBackgroundforcamera0Handler);
                    cameraUtils2=new CameraUtils(cameraManager,"1",MainActivity.this,mBackgroundforcamera1Thread,mBackgroundforcamera1Handler);
                    cameraUtils1.setCameraEventListener(cameraId0EventListener);
                    cameraUtils2.setCameraEventListener(cameraId1EventListener);
                    cameraUtils1.startRecord();
                    cameraUtils2.startRecord();
                    Log.d("TAG", "StartRecord");
                    i++;
                    test_times.setText(String.valueOf(i));
                    mHandler.sendEmptyMessageDelayed(0, 15000);

                    break;
                case 0:
                    cameraUtils1.stopRecord();
                    cameraUtils2.stopRecord();
                    mBackgroundforcamera0Thread.quit();
                    mBackgroundforcamera0Thread=null;
                    mBackgroundforcamera0Handler.removeCallbacksAndMessages(null);
                    mBackgroundforcamera0Handler=null;
                    cameraUtils1=null;
                    cameraUtils2=null;
//                    cameraUtil.videoStop();
//                    cameraUtil1.videoStop();
                    Log.d("TAG", "videoStop");
                    mHandler.sendEmptyMessageDelayed(1, 10000);
                    break;
                case 2:


                    break;
                default:
                    break;
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    //判断设备支持的硬件级别
    private int isHardwareSupported(CameraCharacteristics characteristics) {
        Integer deviceLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
        if (deviceLevel == null) {
            Log.e(TAG, "can not get INFO_SUPPORTED_HARDWARE_LEVEL");
            return -1;
        }
        switch (deviceLevel) {
            case CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL:
                Log.w(TAG, "hardware supported level:LEVEL_FULL");
                break;
            case CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY:
                Log.w(TAG, "hardware supported level:LEVEL_LEGACY");
                break;
            case CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3:
                Log.w(TAG, "hardware supported level:LEVEL_3");
                break;
            case CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED:
                Log.w(TAG, "hardware supported level:LEVEL_LIMITED");
                break;
        }
        return deviceLevel;
    }
//检查授权

//初始化
public void init(){
    cameraManager =(CameraManager) getSystemService(Context.CAMERA_SERVICE);
    test_times=findViewById(R.id.test_times);

    open1=findViewById(R.id.open1);
    open2=findViewById(R.id.open2);
    take1=findViewById(R.id.take1);
    take2=findViewById(R.id.take2);
    recoder1=findViewById(R.id.record1);
    recoder2=findViewById(R.id.record2);
    test_record=findViewById(R.id.test_record);
    open1.setOnCheckedChangeListener(this);
    open2.setOnCheckedChangeListener(this);
    take1.setOnCheckedChangeListener(this);
    take2.setOnCheckedChangeListener(this);
    recoder1.setOnCheckedChangeListener(this);
    recoder2.setOnCheckedChangeListener(this);
    test_record.setOnCheckedChangeListener(this);

}

    @Override
    protected void onDestroy() {
        cameraUtils1.cameraDevice.close();
        cameraUtils2.cameraDevice.close();

        super.onDestroy();
    }

    int i=0;
int i_1=0;


    CameraUtils.CameraEventListener cameraId1EventListener = new CameraUtils.CameraEventListener(){

        @Override
        public void onInitSuccess(CameraDevice cameraDevice) {
            Log.i("camera2", "相机ID1初始化成功");
        }

        @Override
        public void onInitFail(int error) {
            Log.e("camera2", "相机ID1初始化失败"+error);
        }

        @Override
        public void onTakePicture() {
            runOnUiThread(() -> {
                Toast.makeText(MainActivity.this, "拍照成功" , Toast.LENGTH_SHORT).show();
            });

        }

        @Override
        public void onRecorderError(MediaRecorder mr, int what, int extra) {
            Log.e("camera2", "录制过程发生异常  "+what+"  "+extra);
            runOnUiThread(() -> {
                Toast.makeText(MainActivity.this, "录制过程发生异常", Toast.LENGTH_SHORT).show();
            });
        }
    };

    private CameraUtils.CameraEventListener cameraId0EventListener = new CameraUtils.CameraEventListener() {
        @Override
        public void onInitSuccess(CameraDevice cameraDevice) {
            Log.i("camera2", "相机ID0初始化成功");
        }

        @Override
        public void onInitFail(int error) {
            Log.e("camera2", "相机ID0初始化失败  "+error);
        }

        @Override
        public void onTakePicture() {
            runOnUiThread(() -> {
                Toast.makeText(MainActivity.this, "拍照成功 " , Toast.LENGTH_SHORT).show();
            });

        }

        @Override
        public void onRecorderError(MediaRecorder mr, int what, int extra) {
            Log.e("camera2", "录制过程发生异常"+what+"  "+extra);
            runOnUiThread(() -> {
                Toast.makeText(MainActivity.this, "录制过程发生异常", Toast.LENGTH_SHORT).show();
            });
        }
    };

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId()==R.id.open1){
            if (isChecked){

//                cameraUtils1.openCamera("0");


            }
//            if (isChecked&& i==0){
//                cameraUtils1.openCamera("0");
//                i++;
//            }

            if (!isChecked&&i_1==0){
                cameraUtils1.stopPreview();
            }
//            if (!isChecked&&i_1!=0){
//            }
        }
        if (buttonView.getId()==R.id.test_record){
            if (isChecked){
//                cameraUtils1.openCamera("0");
//                cameraUtils2.openCamera("1");
            mHandler.sendEmptyMessageDelayed(1, 1000);}
            if (!isChecked){
                mHandler.removeCallbacksAndMessages(null);
                cameraUtils1.stopRecord();
                cameraUtils2.stopRecord();
            }
        }
        if (buttonView.getId()==R.id.open2){
//            if (isChecked&& j!=0){
//
//                cameraUtils2.startPreview();
//            }
//            if (isChecked&& j==0){
//                cameraUtils2.openCamera("1");
//                j++;
//            }

            if (!isChecked){

                cameraUtils2.stopPreview();
            }
            if (isChecked){

//                cameraUtils2.openCamera("1");

            }
        } if (buttonView.getId()==R.id.take1){
            if (isChecked){
                findViewById(R.id.test_record).setVisibility(View.VISIBLE);
                cameraUtils1=new CameraUtils(cameraManager,"0",MainActivity.this,mBackgroundforcamera0Thread,mBackgroundforcamera0Handler);
                cameraUtils1.openCamera();
            }
           else{
                cameraUtils1.takePhoto();
//                cameraUtils1=null;
            }
//            cameraUtils1.takePhoto();
        } if (buttonView.getId()==R.id.take2){
            if (isChecked){
                cameraUtils2=new CameraUtils(cameraManager,"1",MainActivity.this,mBackgroundforcamera1Thread,mBackgroundforcamera1Handler);
            cameraUtils2.openCamera();
            }
else{
                cameraUtils2.takePhoto();
//                cameraUtils2=null;
            }


        } if (buttonView.getId()==R.id.record1){
            if (isChecked){
                mBackgroundforcamera0Thread=new HandlerThread("0");
                mBackgroundforcamera0Thread.start();
                mBackgroundforcamera0Handler = new Handler(mBackgroundforcamera0Thread.getLooper());
                cameraUtils1=new CameraUtils(cameraManager,"0",MainActivity.this,mBackgroundforcamera0Thread,mBackgroundforcamera0Handler);
                cameraUtils1.startRecord();
            }
            else{
                cameraUtils1.stopRecord();
                mBackgroundforcamera0Thread.quit();
                mBackgroundforcamera0Thread=null;
                mBackgroundforcamera0Handler.removeCallbacksAndMessages(null);
                mBackgroundforcamera0Handler=null;
                cameraUtils1=null;
            }
        } if (buttonView.getId()==R.id.record2){
            if (isChecked){
                mBackgroundforcamera1Thread=new HandlerThread("1");
                mBackgroundforcamera1Thread.start();
                mBackgroundforcamera1Handler = new Handler(mBackgroundforcamera1Thread.getLooper());
                cameraUtils2=new CameraUtils(cameraManager,"1",MainActivity.this,mBackgroundforcamera1Thread,mBackgroundforcamera1Handler);
                cameraUtils2.startRecord();

            }
            else{
                cameraUtils2.stopRecord();
                mBackgroundforcamera1Thread.quit();
                mBackgroundforcamera1Thread=null;
                mBackgroundforcamera1Handler.removeCallbacksAndMessages(null);
                mBackgroundforcamera1Handler=null;
                cameraUtils2=null;

            }
        }
    }
}